<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class Transfer extends Base
{
    protected $name = 'transfer';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function take()
    {
        return $this->hasOne(User::class, 'id', 'take_uid')->field('id,code,nickname,mobile');
    }

    public function getAccountAttr($value)
    {
        return get_pay_way($value);
    }


    public static function add($param, $user, $system): array
    {
        try {
            if (!$system['transfer_open']) {
                return ['code' => -1, 'msg' => '暂未开启转账'];
            }
            //查询账号是否存在
            $take_uid = User::where('code|mobile', $param['take'])->value('id');
            if (empty($take_uid)) {
                return ['code' => -1, 'msg' => '收账会员有误，请检查'];
            }
            if ($take_uid == $user['id']) {
                return ['code' => -1, 'msg' => '收账会员不能为自己'];
            }
            //未开启跨线转账
            if (!$system['transfer_cross_line']) {
                //查询是否为团队
                $up = Relation::where(['uid' => $user->id, 'new_uid' => $take_uid])->count();
                if ($system['transfer_lower_open']) {
                    //开启上下级转账
                    $next = Relation::where(['uid' => $take_uid, 'new_uid' => $user->id])->count();
                    if (!$up && !$next) {
                        return ['code' => -1, 'msg' => '对方与你非同一团队，无法转账'];
                    }
                } else {
                    //未开启上下级转账
                    if (!$up) {
                        return ['code' => -1, 'msg' => '对方与你非同一团队，无法转账'];
                    }
                }
            }
            if (!$param['amount'] || $param['amount'] <= 0) {
                return ['code' => -1, 'msg' => '转账金额错误'];
            }
            if (md5($param['pay_password']) != $user->pay_password) {
                return ['code' => -1, 'msg' => '支付密码错误'];
            }
            if ($param['amount'] < $system['transfer_min']) {
                return ['code' => -1, 'msg' => '最低转账金额：' . $system['transfer_min']];
            }
            if ($system['transfer_bs']) {
                if ($param['amount'] % $system['transfer_bs'] != 0) {
                    return ['code' => -1, 'msg' => '转账倍数：' . $system['transfer_bs']];
                }
            }
            $account = $param['account'];
            $cost = 0;
            if ($system['transfer_cost'] && $system['transfer_cost'] > 0) {
                $cost = round($param['amount'] * $system['transfer_cost'], 4);
                $total = $param['amount'] + $cost;
            } else {
                $total = $param['amount'];
            }
            if ($user->assets->$account < $total) {
                return ['code' => -1, 'msg' => '余额不足'];
            }

            parent::create([
                'account'     => $account,
                'uid'         => $user->id,
                'amount'      => $param['amount'],
                'cost'        => $cost,
                'real_amount' => $total,
                'take_uid'    => $take_uid,
            ]);

            Target::Account()->basics($user->id, $account, $total,$total, '转账', '-')->createNormal();
            Target::Account()->basics($take_uid, $account, $param['amount'], $param['amount'], '收账', '+')->createNormal();

            return ['code' => 1, 'msg' => '已转出'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
